package com.ccx.demo.business.common.service;

import com.alibaba.fastjson.JSON;
import com.utils.IAutoTask;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.Objects;
import java.util.Properties;
import java.util.SortedMap;
import java.util.TreeMap;

import static com.alibaba.fastjson.serializer.SerializerFeature.PrettyFormat;

/**
 * 测试定时任务，生产可以关闭
 *
 * @author 谢长春 on 2020-03-08
 */
@Slf4j
@Service
@Validated
@RequiredArgsConstructor
@ConditionalOnProperty(value = "spring.app.autoTask.services.autoTaskService.enabled", havingValue = "true")
public class AutoTaskService implements IAutoTask {
//    /**
//     * 清除临时文件目录
//     */
//    @Transactional(propagation = Propagation.NEVER)
//    public void clearTempDirectory() {
//        FPath.of(AppConfig.Path.TEMP.file()).deleteAll();
////        FPath.of(System.getProperty("java.io.tmpdir")).deleteAll();
//    }

    /**
     * 获取系统环境变量
     */
    @Transactional(propagation = Propagation.NEVER)
    public void call(final String args) {
        final Properties properties = System.getProperties();
        final SortedMap<String, Object> map = new TreeMap<>();
        properties.forEach((key, value) -> map.put(Objects.toString(key), value));
        log.info("系统环境变量：{}", JSON.toJSONString(map, PrettyFormat));
    }
}
